/**
 * @description：
 * @author: yangyahui01
 * @date: 12/27/24 7:04 PM
 */
public class 合并两个有序的链表 {

    /**
     * 输入：l1 = [1,6,8], l2 = [5,7,9]
     * 输出：[1,5,6,7,8,9]
     */

    public static void main(String[] args) {
        ListNode node = new ListNode(1);
        node.setNext(new ListNode(6));
        node.next.setNext(new ListNode(8));


        ListNode node2 = new ListNode(5);
        node2.setNext(new ListNode(7));
        node2.next.setNext(new ListNode(9));

        ListNode listNode = mergeTwoLists(node, node2);

        System.out.println("11");

    }


    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {

        ListNode p1 = list1;
        ListNode p2 = list2;

        ListNode result = new ListNode(-1);
        ListNode tail = result;

        while (p1 != null && p2 != null) {

            if (p1.val < p2.val) {
                tail.next = p1;
                tail = p1;
                p1 = p1.next;
            } else {
                tail.next = p2;
                tail = p2;
                p2 = p2.next;
            }
        }

        if (p1 != null) tail.next = p1;
        if (p2 != null) tail.next = p2;

        return result.next;
    }


}
